package com.xxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xxx.domain.Login;
import com.xxx.dto.LoginDto;
import com.xxx.mapper.LoginMapper;
import com.xxx.service.ILoginService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xxx.util.AssertUtil;
import com.xxx.util.HttpUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author jh
 * @since 2025-08-30
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    @Value("${ymcc.authurl}")
    private String authUrl;

    @Override
    public Map<String, Objects> login(LoginDto loginDto) {
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        Integer type = loginDto.getType();
        EntityWrapper<Login> wrapper = new EntityWrapper<>();
        wrapper.eq("username", username);
        Login login = selectOne(wrapper);
        AssertUtil.isNotNull(login, "用户不存在");
        AssertUtil.isEquals(login.getType(), type, "用户类型错误");

        String url = String.format(authUrl, login.getClientId(), login.getClientSecret(), username, password);
        //发送http请求,获取token
        String s = HttpUtil.sendPost(url, null);

        return JSONObject.parseObject(s, Map.class);
    }
}
